$(function(){
	initSearch();
	initDatabases();
	initNewsList();
});

function initSearch(){
	$("#search .tab-form").submit(function(){
		var keyword = $(this).find("input.keyword");
		if(keyword.val().length==0){
			alert("请输入关键词！");
			keyword.focus();
			return false;
		}
	});
	
	$("#search button.advance-btn").on("click",function(){
		location.href = $(this).attr("data-url");
	});
}

function initDatabases(){
	$.each($("#database dl.list"),function(index,element){
		$this = $(element);
		$this.empty();
		var data_type = $this.attr("data-type");
		var data_title = $this.attr("data-title");
		$this.append("<dt><a href=\"databases.jspx?f="+data_type+"\">"+data_title+"</a></dt>");
		getDatabase($this,data_type);
	});
}

function getDatabase(dl,data_type){
	$.get("database_gets.jspx?f="+data_type+"&s=10&p=1&format=json",function(data){
		var databases = data.databases;
		if(databases!=undefined){
			$.each(databases,function(index,database){
				$(dl).append("<dd><a href=\"database_"+database.id+".jspx\">"+database.name+"</a></dd>");
			});
		}
	});
}

function initNewsList(){
	$.each($("#information ul.tab-info"),function(index,element){
		$this = $(element);
		$this.empty();
		var data_type = $this.attr("data-type");
		getNews($this,data_type);
	});
	
	var lis = $("#information .nav-pills li");
	if(lis!=undefined && lis.length>0){
		$(lis[0]).addClass("active");
	}
	
	var tabpanes = $("#information .tab-content .tab-pane");
	if(tabpanes!=undefined && tabpanes.length>0){
		$(tabpanes[0]).addClass("active");
	}

}

function getNews(ul,data_type){
	$.get("news_gets.jspx?t="+data_type+"&s=8&p=0&format=json",function(data){
		var newsList = data.newsList;
		if(newsList!=undefined){
			$.each(newsList,function(index,news){
				$(ul).append("<li><span>["+news.addtime+"]</span><a href=\"news_"+news.id+".jspx\">"+news.title+"</a></li>");
			});
		}
	});
}